package com.lvmama.pangolin.service.impl;

import java.util.Date;
import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lvmama.pangolin.common.DateUtil;
import com.lvmama.pangolin.entity.TicketProductGrade;
import com.lvmama.pangolin.enums.Company;
import com.lvmama.pangolin.mybatis.mapper.TicketProductGradeMapper;
import com.lvmama.pangolin.service.TicketProductGradeService;

@Service
public class TicketProductGradeServiceImpl implements TicketProductGradeService {

	@Autowired
	private TicketProductGradeMapper mapper;

	@Override
	@Transactional
	public int insert(TicketProductGrade ticketProductGrade) {
		int result = mapper.insert(ticketProductGrade);
		return result;
	}

	@Override
	public List<TicketProductGrade> query(TicketProductGrade ticketProductGrade) {

		// 默认查询驴妈妈产品
		if (StringUtils.isEmpty(ticketProductGrade.getCompanyCode())) {
			ticketProductGrade.setCompanyCode(Company.LMMXC.getCode());
		}
		if(StringUtils.startsWith(ticketProductGrade.getCompanyCode(), "LMM")){
			ticketProductGrade.setTableSuffix("lmm");
		}else{
			// 设置ticket_product_${tableSuffix}表后缀. eg.ticket_product_lmm
			ticketProductGrade.setTableSuffix(StringUtils.lowerCase(ticketProductGrade.getCompanyCode()));
		}
		// 获取前一天的数据
		ticketProductGrade.setCreateDate(DateUtil.getCurrentymd(DateUtils.addDays(new Date(), -1)));

		List<TicketProductGrade> resultList = mapper.query(ticketProductGrade);

		return resultList;

	}

	@Override
	public PageInfo<TicketProductGrade> queryByPage(TicketProductGrade ticketProductGrade, int pageNumber,
			int pageSize) {

		PageHelper.startPage(pageNumber, pageSize);

		// 默认查询驴妈妈产品
		if (StringUtils.isEmpty(ticketProductGrade.getCompanyCode())) {
			ticketProductGrade.setCompanyCode(Company.LMMXC.getCode());
		}
		if(StringUtils.startsWith(ticketProductGrade.getCompanyCode(), "LMM")){
			ticketProductGrade.setTableSuffix("lmm");
		}else{
			// 设置ticket_product_${tableSuffix}表后缀. eg.ticket_product_lmm
			ticketProductGrade.setTableSuffix(StringUtils.lowerCase(ticketProductGrade.getCompanyCode()));
		}
		// 获取前一天的数据
		ticketProductGrade.setCreateDate(DateUtil.getCurrentymd(DateUtils.addDays(new Date(), -1)));

		List<TicketProductGrade> resultList = mapper.query(ticketProductGrade);
		PageInfo<TicketProductGrade> pageInfo = new PageInfo<TicketProductGrade>(resultList);

		return pageInfo;
	}

	@Override
	public int deleteSameObject(TicketProductGrade ticketProductGrade) {
		// TODO Auto-generated method stub
		return mapper.deleteSameObject(ticketProductGrade);
	}

}
